package club.janna.concurrency.example.singleton;

import club.janna.concurrency.annotation.ThreadSafe;

/**
 * 单例模式-懒汉模式
 * 第一次使用时进行初始化
 * @Auther: guopanbo
 * @Date: 2018/8/1 21:49
 * @Description:
 */
@ThreadSafe
public class SingletonExample5 {

    private SingletonExample5() {}

    private static volatile SingletonExample5 instance = null;


    /**
     * 使用volatile禁止指令重排序，保证线程安全
     * @return
     */
    public static SingletonExample5 getInstance() {
        if(instance == null) {
            synchronized (SingletonExample5.class) {
                if(instance == null)
                    instance = new SingletonExample5();
            }
        }
        return instance;
    }
}
